package Util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

public class OpArquivos {
	
	public static ArrayList<String> leArquivo(String path) throws IOException {
        
		ArrayList<String> strings = new ArrayList<String>();
        BufferedReader br;
       
        br = new BufferedReader(new FileReader(path));
        while (br.ready()) {
                strings.add(br.readLine());
        }
        br.close();

        return strings;
	}
	
	public static void escreveArquivo(String path, ArrayList<String> strings) throws IOException {

        BufferedWriter bw;
       
        criaPasta(path);

        bw = new BufferedWriter(new FileWriter(path));
        bw.write(strings.get(0));
        
        for (int i = 1; i < strings.size(); i++) {
                bw.newLine();
                bw.write(strings.get(i));
        }
        bw.close();

	}

	 private static void criaPasta(String destinationPath){
         File f;
        
         String[] path = destinationPath.split("/");
         String folders = "";

         for (int i = 0; i < path.length - 1; i++) {
                 folders += path[i] + ((i == path.length - 2) ? "" : "/");
         }
        
         if (!(f = new File(folders)).exists()){
                 f.mkdir();
         }
                
 }



}
